package com.os.sys.common.utility;

import com.github.pagehelper.PageHelper;
import com.os.sys.common.auth.CommonAuthService;
import com.os.sys.common.service.RedisCacheService;
import com.os.sys.model.ILoginUser;
import com.os.sys.service.LogService;
import com.os.sys.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/5/26.
 */
@Service
public class PubClass {
    @Resource
    private CommonAuthService commonAuthService;
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;
    @Resource
    private RedisCacheService redisCacheService;

    //region 登录用户
    // 获得登录用户
    public ILoginUser getLoginUser() {
        return commonAuthService.getLoginUser();
    }

    // 获得登录用户名
    public String getLoginUserName() {
        ILoginUser info = getLoginUser();
        if (info == null) {
            return "";
        }
        return info.getLoginUserName();

    }

    public Long getLoginUserId() {
        ILoginUser info = getLoginUser();
        if (info == null) {
            return 0l;
        }
        return Long.valueOf(info.getLoginUserID());

    }

    public String getLoginUserType() {
        ILoginUser info = getLoginUser();
        if (info == null) {
            return "";
        }
        return info.getLoginUserType();

    }
    //endregion

    //region 分页逻辑
    public Pageable createPagenation() {
        return createPagenation("id");
    }

    public Pageable createPagenation(String sortKey) {
        int page = WebClass.requestToInt("pageNo");
        int pageSize = WebClass.requestToInt("pageSize");

        if (page > 0 && pageSize > 0) {
            Sort sort = new Sort(Sort.Direction.DESC, sortKey);
            return PageRequest.of(page - 1, pageSize, sort);
        } else {
            return Pageable.unpaged();
        }
    }

    public void createPagenationForMybaits() {
        int page = WebClass.requestToInt("pageNo");
        int pageSize = WebClass.requestToInt("pageSize");

        if (page > 0 && pageSize > 0) {
            PageHelper.startPage(page, pageSize);
        }
    }
    //endregion

    //region 权限验证

    //region 验证power
    public boolean chkLoginUserPower(String powerKeys) {
        return chkLoginUserPower(powerKeys.split(","), true);
    }

    public boolean chkLoginUserPower(String powerKeys, boolean isOR) {
        return chkLoginUserPower(powerKeys.split(","), isOR);
    }

    // 检查当前登录用户是否具有某些权限,默认是or逻辑
    public boolean chkLoginUserPower(String[] powerKey) {
        return chkLoginUserPower(powerKey, true);
    }

    // 检查当前登录用户是否具有某些权限
    public boolean chkLoginUserPower(String[] powerKey, boolean isOR) {
        if (powerKey == null || powerKey.length == 0) {
            return true;
        }
        ILoginUser info = commonAuthService.getLoginUser();
        List<String> listPower = info.getLoginUserRolesPower();

        boolean flag = true;
        if (isOR) {
            flag = false;
            for (String p : powerKey) {
                if (listPower.contains(p)) {
                    flag = true;
                    break;
                }
            }
        } else {
            for (String p : powerKey) {
                if (!listPower.contains(p)) {
                    flag = false;
                    break;
                }
            }
        }

        return flag;
    }
    //endregion

    //region 验证role
    // 检查当前登录用户是否具有某些权限,默认是or逻辑
    public boolean chkLoginUserRole(String roleKeys) {
        return chkLoginUserRole(roleKeys.split(","), true);
    }

    public boolean chkLoginUserRole(String roleKeys, boolean isOR) {
        return chkLoginUserRole(roleKeys.split(","), isOR);
    }

    public boolean chkLoginUserRole(String[] roleKey) {
        return chkLoginUserRole(roleKey, true);
    }

    // 检查当前登录用户是否具有某些角色
    public boolean chkLoginUserRole(String[] roleKey, boolean isOR) {
        if (roleKey == null || roleKey.length == 0) {
            return true;
        }
        ILoginUser info = commonAuthService.getLoginUser();
        List<String> listRole = info.getLoginUserRolesKey();

        boolean flag = true;
        if (isOR) {
            flag = false;
            for (String r : roleKey) {
                if (listRole.contains(r)) {
                    flag = true;
                    break;
                }
            }
        } else {
            for (String r : roleKey) {
                if (!listRole.contains(r)) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }
    //endregion
    //endregion

    //region 日志
    public XJsonInfo addLog(String logType, String content) {
        return logService.addLog(logType, content);
    }

    public void showLog(String content) {
        Logger logger = LoggerFactory.getLogger(PubClass.class.getName());
        logger.info(content);
    }

    public void showLog(String content, String name) {
        Logger logger = LoggerFactory.getLogger(name);
        logger.info(content);
    }

    public void showLog(String content, Class<?> clazz) {
        Logger logger = LoggerFactory.getLogger(clazz);
        logger.info(content);
    }

    public void showErrorLog(String content, Class<?> clazz) {
        Logger logger = LoggerFactory.getLogger(clazz);
        logger.error(content);
    }
    //endregion

    //region 缓存
    public void setCache(String key, Object obj) {
        redisCacheService.set(key, obj);
    }

    public void setCache(String key, Object obj, int mins) {
        redisCacheService.set(key, obj, mins);
    }

    public Object getCache(String key) {
        return redisCacheService.get(key);
    }

    public void removeCache(String key) {
        redisCacheService.delete(key);
    }

    public boolean hasCache(String key) {
        return redisCacheService.hasKey(key);
    }

    public void updateCacheTime(String key, long expireTime) {
        redisCacheService.expireTime(key, expireTime);
    }
    //endregion
}
